import { createWebHistory, createRouter , createRouterMatcher } from "vue-router";

import auto from "./auto-router.js"
import app from "./app.js"
import index from '../view/home/index.vue'



var routers = [
    ...auto,
    {
        path: '/',
        meta: {tab: "首页"},
        component: index
    }, {
        path: '/tools',
        meta: {tab: "工具"},
        component: () => import('../view/home/tools.vue')
    },
    ...app
];

var setViewsMetaTab = (router, pTabName) => {
    var views = router.views
    if (!views || !views.length) {
        return [];
    }

    for (let i = 0, len = views.length; i < len; i++) {
        var v = views[i];
        if (!v.meta.tab) {
            v.meta.tab = pTabName || "";
        }
        setViewsMetaTab(v, v.meta.tab);
    }
}


var getViews = router => {
    var views = router.views
    if (!views || !views.length) {
        return [];
    }

    var ret = [];
    ret.addAll(views);
    for (let i = 0, len = views.length; i < len; i++) {
        ret.addAll(getViews(views[i]));
    }
    return ret;
}


var views = [];
for (let i = 0, len = routers.length; i < len; i++) {
    var router = routers[i];
    setViewsMetaTab(router);
    views.addAll(getViews(router));
}

routers.addAll(views);


routers.push({
    path :"/:catchAll(.*)",
    component: import ("../component/no-found.vue")
})


export default createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: routers
});

